﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OreSystem.Entities;
using OreSystem.BLL;
namespace OreSystemWeb.Areas.Admin.Controllers
{
    public class ItemController : Controller
    {
        //
        // GET: /Admin/Item/
        private ItemBLL itemBll = new ItemBLL();
        private CarBLL carBll = new CarBLL();
        public ActionResult Index(int id)
        {
            ViewData["type"] = id;
            var CarID = carBll.GetCarByType(id);
            if (CarID.Count <= 0)
	        {
                CarID.Add(new Car { ID = 0, CarNo = "没有信息请添加" });
	        }
            var items = from i in CarID select new SelectListItem { Text = i.CarNo, Value = i.ID.ToString() };
            ViewData["CarID"] = items;
            return View();
        }
        public ActionResult GetListByPage(int type,int page ,int rows)
        {
            int totals=0;
            var lists = itemBll.GetListByPage(type, page-1, rows, out totals);
            return Json(new {total=totals,rows=lists });

        }
        //
        // GET: /Admin/Item/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Admin/Item/Create

        public ActionResult Create(int id)
        {
            return Json(new Item { Type = id, CreateDate = DateTime.Now },JsonRequestBehavior.AllowGet);
        } 

        //
        // POST: /Admin/Item/Create

        [HttpPost]
        public ActionResult Create(Item item)
        {
            try
            {
                // TODO: Add insert logic here
                itemBll.Save(item);
                return Json(new {success=true,msg="添加成功" },JsonRequestBehavior.AllowGet);
            }
            catch(Exception e)
            {
                return Json(new {success=false,msg=e.Message },JsonRequestBehavior.AllowGet);
            }
        }
        
        //
        // GET: /Admin/Item/Edit/5
 
        public ActionResult Edit(int id)
        {
            return Json(itemBll.GetItemById(id),JsonRequestBehavior.AllowGet);           
        }

        //
        // POST: /Admin/Item/Edit/5

        [HttpPost]
        public ActionResult Edit(Item item)
        {
            try
            {
                // TODO: Add insert logic here
                itemBll.Save(item);
                return Json(new { success = true, msg = "添加成功" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return Json(new { success = false, msg = e.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        //
        // GET: /Admin/Item/Delete/5
        [HttpPost] 
        public ActionResult Delete(int id)
        {
            try
            {
                itemBll.Delete(new Item { ID = id });
                return Json(new { success=true,msg="删除成功！" },JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {

                return Json(new { success=false,msg="删除失败"+e.Message },JsonRequestBehavior.AllowGet);
            }
        }

        //
        public ActionResult Deletes(IList<int> idlist)
        {
            try
            {
                itemBll.Deletes(idlist.Cast<object>().ToList());
                return Json(new { success = true, msg = "删除成功" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return Json(new {success=false,msg="删除失败" +e.Message},JsonRequestBehavior.AllowGet);
                
            }
        }
        
    }
}
